package com.newbingo.action;

import com.googlecode.jsonplugin.annotations.JSON;
import com.newbingo.dto.GameTableDto;
import com.newbingo.entity.Game;
import com.newbingo.entity.GameOperationTime;
import com.newbingo.entity.GameTable;
import com.newbingo.service.imple.IGameTableService;
import com.newbingo.util.Pager;
import com.opensymphony.xwork2.ActionSupport;

public class GameTableAction extends ActionSupport {

	/**
	 * 页面添加，修改注入的属性 gameTable.gtno Integer gameTable.name gameTable.website
	 * gameTable.status gameTable.rule gameTable.game.gameno Integer
	 * gameTable.operationTime.openTime gameTable.operationTime.closeTime
	 * gameTable.operationTime.drawLotteryInterval
	 * gameTable.operationTime.endBettingTimeout
	 */

	private IGameTableService gametableService;
	private GameTable gameTable;
	private Game game = null;

	private int start;
	private int limit = 10;
	private Pager pager;
	/**
	 * Integer gtno;              //游戏桌编号 
	 * String deskName;           //桌名 
	 * String website;            //网址 
	 * String status;             //状态 
	 * String openTime;           //开场时间 
	 * String closeTime;          //关场时间 
	 * String endBettingTimeout;  //投注截止时间 
	 * String drawLotteryInterval;//每期间隔时间
	 */
	private Boolean success = false;
	//游戏桌的DTO对象
	private GameTableDto gameTableDto;
	private Integer gtno;

	/** *********操作方法********** */

	/**
	 * 查询
	 */
	public String findByPage() {
		this.setPager(gametableService.findByPage(start, limit, game
				.getGameno()));
		success = true;
		return SUCCESS;
	}

	/**
	 * 根据Id查询
	 */
	public String findById() {
		gameTableDto=gametableService.findById(gtno);
		success=true;
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	public String create() {
		if (gameTable != null) {
			gameTable.setStatus("closed");
		}
		gametableService.save(gameTable);
		this.setGameTable(null);
		return SUCCESS;

	}

	/**
	 * 删除
	 */
	public String delete() {
		gametableService.delete(gameTable);
		return SUCCESS;
	}

	/**
	 * 修改
	 */
	public String merge() {
		GameTable gt = gametableService.findByGameTableId(gameTable.getGtno());
		if (gameTable == null) {
			success = false;
		} else {
			gt.setWebsite(gameTable.getWebsite());
			GameOperationTime go = new GameOperationTime();
			go.setOpenTime(gameTable.getOperationTime().getOpenTime());
			go.setCloseTime(gameTable.getOperationTime().getCloseTime());
			go.setEndBettingTimeout(gameTable.getOperationTime()
					.getEndBettingTimeout());
			go.setDrawLotteryInterval(gameTable.getOperationTime()
					.getDrawLotteryInterval());
			gt.setOperationTime(go);
			gametableService.merge(gt);
			success = true;
		}
		return SUCCESS;
	}

	/**
	 * 游戏开关
	 */
	public String updateStatus() {
		GameTable gdesk = gametableService.findByGameTableId(gameTable
				.getGtno());
		if (gameTable == null) {
			success = false;
		} else {
			gdesk.setStatus(gameTable.getStatus());
			gametableService.merge(gdesk);
			success = true;
		}
		return SUCCESS;
	}

	@JSON(serialize = false)
	public Game getGame() {
		return game;
	}

	public void setGame(Game game) {
		this.game = game;
	}
	
	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	@JSON(serialize = false)
	public GameTable getGameTable() {
		return gameTable;
	}

	public void setGameTable(GameTable gameTable) {
		this.gameTable = gameTable;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	@JSON(serialize = false)
	public IGameTableService getGametableService() {
		return gametableService;
	}

	public void setGametableService(IGameTableService gametableService) {
		this.gametableService = gametableService;
	}

	public GameTableDto getGameTableDto() {
		return gameTableDto;
	}

	public void setGameTableDto(GameTableDto gameTableDto) {
		this.gameTableDto = gameTableDto;
	}
	public Integer getGtno() {
		return gtno;
	}

	public void setGtno(Integer gtno) {
		this.gtno = gtno;
	}

}
